
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>自定义验证码 - Mr.Song</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="自定义验证码1234567891011121314151617181920212223242526272829303132333435363738394041424344454647#自定义图片验证,"> 
    <meta name="author" content="Mr.Song"> 
    
    <link rel="icon" href="/song_b/img/song.png"> 
    
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">

    
<link rel="stylesheet" href="/song_b/css/diaspora.css">

	<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script>
         (adsbygoogle = window.adsbygoogle || []).push({
              google_ad_client: "ca-pub-8691406134231910",
              enable_page_level_ads: true
         });
    </script>
    <script async custom-element="amp-auto-ads"
        src="https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js">
    </script>
<meta name="generator" content="Hexo 4.2.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">Mr.Song</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://gitee.com/song_shen_olympic/song_b.git"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">自定义验证码</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">自定义验证码</h1>
        <div class="stuff">
            <span>四月 26, 2020</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/song_b/tags/python/" rel="tag">python</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="自定义验证码"><a href="#自定义验证码" class="headerlink" title="自定义验证码"></a>自定义验证码</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">#自定义图片验证码</span><br><span class="line">class MyCode(View):</span><br><span class="line"></span><br><span class="line">	#定义rgb随机颜色</span><br><span class="line">	def get_random_color(self):</span><br><span class="line"></span><br><span class="line">		R &#x3D; random.randrange(255)</span><br><span class="line">		G &#x3D; random.randrange(255)</span><br><span class="line">		B &#x3D; random.randrange(255)</span><br><span class="line"></span><br><span class="line">		return (R,G,B)</span><br><span class="line"></span><br><span class="line">	#定义图片视图</span><br><span class="line">	def get(self,request):</span><br><span class="line">		#画布</span><br><span class="line">		img_size &#x3D; (120,50)</span><br><span class="line">		#定义图片对象</span><br><span class="line">		image &#x3D; Image.new(&#39;RGB&#39;,img_size,&#39;white&#39;)</span><br><span class="line">		#定义画笔</span><br><span class="line">		draw &#x3D; ImageDraw.Draw(image,&#39;RGB&#39;)</span><br><span class="line">		source &#x3D; &#39;0123456789abcdefghijk&#39;</span><br><span class="line">		#接收容器</span><br><span class="line">		code_str &#x3D; &#39;&#39;</span><br><span class="line">		#进入循环绘制</span><br><span class="line">		for i in range(4):</span><br><span class="line">			#获取字母颜色</span><br><span class="line">			text_color &#x3D; self.get_random_color()</span><br><span class="line">			#获取随机下标</span><br><span class="line">			tmp_num &#x3D; random.randrange(len(source))</span><br><span class="line">			#随机字符串</span><br><span class="line">			random_str &#x3D; source[tmp_num]</span><br><span class="line">			#装入容器</span><br><span class="line">			code_str +&#x3D; random_str</span><br><span class="line">			#绘制字符串</span><br><span class="line">			draw.text((10+30*i,20),random_str,text_color)</span><br><span class="line">		#获取缓存区</span><br><span class="line">		buf &#x3D; io.BytesIO()</span><br><span class="line">		#将临时图片保存到缓冲</span><br><span class="line">		image.save(buf,&#39;png&#39;)</span><br><span class="line">		#保存随机码</span><br><span class="line">		r.set(&#39;code&#39;,code_str)</span><br><span class="line">		#保存session</span><br><span class="line">		request.session[&#39;code&#39;] &#x3D; code_str</span><br><span class="line"></span><br><span class="line">		print(r.get(&#39;code&#39;))</span><br><span class="line"></span><br><span class="line">		return HttpResponse(buf.getvalue(),&#39;image&#x2F;png&#39;)</span><br></pre></td></tr></table></figure>


<p><img src="/hugo_blog/0d8a90abc2d78eaa5e01d3fb7f57e6da.jpg" alt="每日一图"></p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="true">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title='0' data-url='/song_b//img/002.mp3'></li>
                        
                    
                </ul>
            
        </div>
        
    <div id='gitalk-container' class="comment link"
		data-enable='true'
        data-ae='true'
        data-ci=''
        data-cs=''
        data-r=''
        data-o=''
        data-a=''
        data-d='false'
    >查看评论</div>


    </div>
    
        <div class='side'>
			<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#自定义验证码"><span class="toc-number">1.</span> <span class="toc-text">自定义验证码</span></a></li></ol>	
        </div>
    
</div>


    </div>
</div>
</body>

<script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/song_b/js/plugin.js"></script>
<script src="/song_b/js/typed.js"></script>
<script src="/song_b/js/diaspora.js"></script>


<link rel="stylesheet" href="/song_b/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/song_b/photoswipe/default-skin/default-skin.css">


<script src="/song_b/photoswipe/photoswipe.min.js"></script>
<script src="/song_b/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
